<template>
  <el-dialog
    v-model="state.dialogFormVisible"
    :title="t('mail.testInfo')"
    custom-class="dialog-min-width"
    width="500"
    :destroy-on-close="true"
  >
    <el-form
      v-loading="loading"
      ref="formRef"
      :model="formEdit"
      :rules="rules"
      label-width="120px"
    >
      <el-row>
        <el-col :lg="24" :md="24" :sm="24" :xs="24">
          <el-form-item :label="t('mail.sendTo')" prop="sendTo">
            <el-input
              v-model="formEdit.sendTo"
              maxlength="256"
              :placeholder="t('mail.multipleEmailSeparated')"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="24" :md="24" :sm="24" :xs="24">
          <el-form-item :label="t('mail.sendCC')" prop="sendCC">
            <el-input v-model="formEdit.sendCC" maxlength="256" clearable>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="24" :md="24" :sm="24" :xs="24">
          <el-form-item :label="t('mail.subject')" prop="subject">
            <el-input v-model="formEdit.subject" maxlength="256" clearable>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="24" :md="24" :sm="24" :xs="24">
          <el-form-item :label="t('mail.content')">
            <el-input
              v-model="formEdit.content"
              type="textarea"
              rows="6"
              maxlength="4000"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="state.dialogFormVisible = false">{{
          t("common.cancelButton")
        }}</el-button>
        <el-button type="primary" @click="handleSubmit">{{
          t("common.submitButton")
        }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import utils from "../../../utils/utils";
import apiMail from "../api/mail";
import apiAccount from "../api/account";
import { useI18n } from "vue-i18n";
const { t } = useI18n({ useScope: "global" });
const formRef = ref();
const loading = ref(false);

const formEditOriginal = () => {
  return {
    sendTo: "",
    sendCC: "",
    subject: "",
    content: "",
  };
};
const formEdit = reactive(formEditOriginal());

const rules = reactive({
  sendTo: [
    {
      required: true,
      message: () => t("common.required"),
      trigger: "blur",
    },
  ],
  //   sendCC: [
  //     {
  //       required: true,
  //       message: () => t("common.required"),
  //       trigger: "blur",
  //     },
  //   ],
  subject: [
    {
      required: true,
      message: () => t("common.required"),
      trigger: "blur",
    },
  ],
  content: [
    {
      required: true,
      message: () => t("common.required"),
      trigger: "blur",
    },
  ],
});

const state = reactive({
  dialogFormVisible: false,
});

const emit = defineEmits<{
  (event: "on-submit-success"): void;
}>();

const handleSubmit = async () => {
  await formRef.value.validate();

  let contentBase64 = utils.base64Encode(formEdit.content);

  loading.value = true;
  apiMail
    .testMail(formEdit.sendTo, formEdit.sendCC, formEdit.subject, contentBase64)
    .then((response: any) => {
      loading.value = false;
      if (response.data.code == 200) {
        state.dialogFormVisible = false;
        utils.infoNotification(t("common.submitSuccess"));
        emit("on-submit-success");
      }
    })
    .catch((err: any) => {
      loading.value = false;
      utils.errorNotification(t("common.submitException"), err);
    });
};

const show = () => {
  // console.log(row);
  Object.assign(formEdit, formEditOriginal());
  // 重置表单校验
  if (formRef.value) {
    formRef.value.resetFields();
  }
  loading.value = false;
  state.dialogFormVisible = true;
};

defineExpose({
  show,
});
</script>
